class Solution {
    public String convert(String s, int numRows) {
        if (numRows == 1) {
            return s;
        }
        int d = 2*numRows - 2;
        StringBuilder result = new StringBuilder();
        // 第一行
        for (int i = 0; i < s.length(); i += d) {
            char c = s.charAt(i);
            result.append(c);
        }
        // 中间行
        for (int i = 1; i < numRows - 1; i++) {
            int left = i;
            int right = d - left;
            while (left < s.length()) {
                char c1 = s.charAt(left);
                result.append(c1);
                if (right < s.length()) {
                    char c2 = s.charAt(right);
                    result.append(c2);
                }
                left += d;
                right += d;
            }
        }

        // 最后一行
        for (int i = numRows - 1; i < s.length(); i += d) {
            char c = s.charAt(i);
            result.append(c);
        }

        return result.toString();
    }
}